package com.weixin.paleituo.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.fragment.TemplateAlbumFragment;
import com.weixin.paleituo.fragment.TemplateDecorationFragment;
import com.weixin.paleituo.fragment.TemplateGoodsFragment;
import com.weixin.paleituo.fragment.TemplateInfoFragment;

import java.util.HashMap;

/**
 * 共享家样板间详情页面切换Adapter
 *
 * @author fengzhen
 * @version v1.0, 2018/4/20
 */
public class SharedTemplatePagerAdapter extends FragmentPagerAdapter {
    private String[] mTitles;
    private TemplateDetailsBean mTemplate;
    private HashMap<Integer, Fragment> mFragmentMap = new HashMap<>();

    public SharedTemplatePagerAdapter(FragmentManager fm, String[] titles, TemplateDetailsBean template) {
        super(fm);
        mTitles = titles;
        mTemplate = template;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if (mFragmentMap.containsKey(position)) {
            fragment = mFragmentMap.get(position);
        } else {
            switch (position) {
                case 0:
                    fragment = TemplateInfoFragment.newInstance(mTemplate);
                    break;
                case 1:
                    fragment = TemplateAlbumFragment.newInstance(mTemplate);
                    break;
                case 2:
                    fragment = TemplateDecorationFragment.newInstance(mTemplate);
                    break;
                case 3:
                    fragment = TemplateGoodsFragment.newInstance(mTemplate);
                    break;
            }
            mFragmentMap.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mTitles != null ? mTitles.length : 0;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }


}
